package fh.app4stud.mensa.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import fh.app4stud.mensa.R;
import fh.app4stud.mensa.database.MenuDbAdapter;

import java.util.ArrayList;

/**
 * Central point for Location settings
 *
 * Created by IntelliJ IDEA.
 * User: ifi
 * Date: 22.04.11
 * Time: 07:28
 * To change this template use File | Settings | File Templates.
 */
public class SettingsPreferences extends Activity {

    private SharedPreferences prefs;
    private Spinner citiesSpinner;
    private Button seeLocationsButton;
    private ArrayList<String> listOfCities;
    private ArrayAdapter<String> aa_listOfCities;
    private MenuDbAdapter menuDbHelper;
    private boolean checkByCity;

    public static final String[] PREFEREDLOCATIONSARRAY = new String[]{SettingsPreferences.PREF_LOCATION1, SettingsPreferences.PREF_LOCATION2, SettingsPreferences.PREF_LOCATION3, SettingsPreferences.PREF_LOCATION4} ;
    public static final String PREF_CHECK_BY_CITY = "PREF_CHECK_BY_CITY"; //if list should be filtered by city
    public static final String PREF_CHECK_BY_RANGE = "PREF_CHECK_BY_RANGE";//if list of loc. should be filtered by range
    public static final String PREF_MAX_DIST = "PREF_MAX_DIST"; //for saving the index of distance
    public static final String PREF_CITY_INT = "PREF_CITY_INT";//for saving the index of city
    public static final String PREF_CITY_NAME = "PREF_CITY_NAME";//for saving the index of city
    public static final String CURRENT_LOCATION = "CURRENT_LOCATION"; //when looking at menus this is the location
    public static final String CURRENT_DAY = "CURRENT_DAY"; //current day of the week, if sunday=1
    public static final String PREF_LOCATION1 = "PREF_LOCATION1";//for the list of preferred locaitons always shown on start
    public static final String PREF_LOCATION2 = "PREF_LOCATION2";//for the list of preferred locaitons always shown on start
    public static final String PREF_LOCATION3 = "PREF_LOCATION3";//for the list of preferred locaitons always shown on start
    public static final String PREF_LOCATION4 = "PREF_LOCATION4";//for the list of preferred locaitons always shown on start
    public static final String CURRENT_LATITUDE = "CURRENT_LATITUDE"; //dummy position
    public static final String CURRENT_LONGITUDE = "CURRENT_LONGITUDE";//dummy position


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_list_preferences);

        Context context = getApplicationContext();
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
         
        menuDbHelper = new MenuDbAdapter(this);
        menuDbHelper.open();

        listOfCities = new ArrayList<String>();
        citiesSpinner = (Spinner)findViewById(R.id.settings_list_spinner_city);
        seeLocationsButton = (Button)findViewById(R.id.settings_button_show);

        populateCitySpinner();
        HookUpButtons();

    }

    @Override
    protected void onStop() {
        super.onStop();
        saveState();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        UpdateUIPreferences();
    }

    private void populateCitySpinner() {

        Cursor mCursor = menuDbHelper.fetchCityNames();
        int cityKey = mCursor.getColumnIndexOrThrow(MenuDbAdapter.KEY_CITY);
        if(mCursor.moveToFirst()){
            while (!mCursor.isAfterLast()) {
                 String city = mCursor.getString(cityKey);
                 listOfCities.add(city);
                mCursor.moveToNext();
            }
            mCursor.close();
        }
        aa_listOfCities = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listOfCities);
        int spinner_dd_item = android.R.layout.simple_spinner_dropdown_item;
        aa_listOfCities.setDropDownViewResource(spinner_dd_item);
        citiesSpinner.setAdapter(aa_listOfCities);

    }

    private void saveState() {

            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt(PREF_CITY_INT, citiesSpinner.getSelectedItemPosition());
            editor.putString(PREF_CITY_NAME, citiesSpinner.getSelectedItem().toString());
            editor.commit();

        }

    private void UpdateUIPreferences() {

        int cityIndex = prefs.getInt(PREF_CITY_INT, 0);
        citiesSpinner.setSelection(cityIndex);
    }

    private void HookUpButtons() {

        seeLocationsButton = (Button) findViewById(R.id.settings_button_show);

        seeLocationsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                saveState();

                Intent locationList = new Intent(SettingsPreferences.this, LocationsList.class);
                locationList.putExtra(PREF_CHECK_BY_CITY, checkByCity);

                locationList.putExtra(PREF_CITY_NAME, prefs.getString(PREF_CITY_NAME, ""));

                startActivity(locationList);
            }
        });

        /*
        Button cancelButton = (Button) findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                SettingsPreferences.this.setResult(RESULT_CANCELED);
                finish();
            }
        });
        */
    }

}
